package com.doge.walknovel.business.reader.menus

import android.graphics.Typeface
import com.doge.wnpro.R
import com.doge.walknovel.utils.*

object ReaderHelper {
    val typeFaceFutura by lazy { Typeface.createFromAsset(appContext.assets, "Handwritten.ttf")!! }
    val typeFaceMonaco by lazy { Typeface.createFromAsset(appContext.assets, "Standard.ttf")!! }
    const val bgWhite = "#ffffff"
    const val bgYellow = "#ffeed4"
    const val bgGreen = "#e0ecd9"
    private const val folder = "reader"
    private const val keySize = "textSize"
    private const val keyBgColor = "bgColor"
    private const val keyLight = "light"
    private const val keyTypeface = "typeface"

    private const val defaultBgColor = "#ffffff"
    private const val defaultTextSize = 16
    private const val defaultLight = true
    private val defaultTypeface = typeFaceFutura
    fun initialized() {
        currentSize = spReadInt(keySize, defaultTextSize, folder)
        currentBgColor = spReadString(keyBgColor, defaultBgColor, folder) ?: defaultBgColor
        currentLight = spReadBoolean(keyLight, defaultLight, folder)
        currentTypeface = diskKeyToTypeface(spReadString(keyTypeface, null, folder))
    }

    interface Listener {
        fun onSizeChanged(value: Int) {}
        fun onBgColorChanged(value: String) {}
        fun onLightChanged(value: Boolean) {}
        fun onTypefaceChanged(value: Typeface) {}
    }

    val light: Boolean get() = currentLight
    val bgColor: String get() = currentBgColor
    val size: Int get() = currentSize
    val typeface: Typeface get() = currentTypeface
    private var currentLight: Boolean = defaultLight
    private var currentBgColor: String = defaultBgColor
    private var currentSize = defaultTextSize
    private var currentTypeface = defaultTypeface
    private var callback: Listener? = null

    fun setListener(listener: Listener?) {
        callback = listener
    }

    fun changeLight(value: Boolean) {
        require(isMainThread())
        currentLight = value
        callback?.onLightChanged(value)
        runOnIO {
            spWrite(keyLight, value, folder)
        }
    }

    fun changeSize(value: Int) {
        require(isMainThread())
        currentSize = value
        callback?.onSizeChanged(value)
        runOnIO {
            spWrite(keySize, value, folder)
        }
    }

    fun changeBgColor(color: String) {
        require(isMainThread())
        currentBgColor = color
        callback?.onBgColorChanged(color)
        runOnIO {
            spWrite(keyBgColor, color, folder)
        }
    }

    fun changeTypeFace(typeface: Typeface) {
        require(isMainThread())
        currentTypeface = typeface
        callback?.onTypefaceChanged(typeface)
        runOnIO {
            spWrite(keyTypeface, typefaceToDiskKey(typeface), folder)
        }
    }

    private fun typefaceToDiskKey(typeface: Typeface): String {
        return when (typeface) {
            typeFaceFutura -> getString(R.string.handwritten)
            typeFaceMonaco -> getString(R.string.standard)
            else -> getString(R.string.system)
        }
    }

    private fun diskKeyToTypeface(typeface: String?): Typeface {
        return when (typeface) {
            getString(R.string.system) -> Typeface.DEFAULT
            getString(R.string.standard) -> typeFaceMonaco
            else -> typeFaceFutura
        }
    }
}